/**      
 * @file		constFunction.cpp
 * @description	Demonstrates the use of sending a whole
 *				array as a parameter to a function. This
 *				doesn't pass the array by value. It passes arrays similar
 *				to pass by reference even though the & isn't used.
 *				We can modify the elements of the array.
 *				Arrays are special. The example also demonstrates the 
 *				use of const parameters.
 * @course		CSCI 123 Section 00000
 * @assignment 
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		4.0
 */
#include <iostream>
#include <cctype>
using namespace std;

const int NUMBER_OF_GRADES = 3;

/**
 * Gets the grades from the user
 * @pre		the grades have been initialized
 * @post	the grade values have been set by the user  
 * @param	grades the list to store the grades
 */
void getGrades(char grades[]);

/**
 * Displays the grades to standard output
 * @pre		all the grade values have been set
 * @post	the grades have been display (using standard output)
 *		to the user
 * @param	grades the list of grades to display to the user
 */
void displayGrades(const char grades[]);

/**
 * Displays the grades to standard output
 * @pre		grades have been initialized and values have been set
 * @post	the grades averaged
 * @param	grades the list of grades to average
 */
double averageGrades(char grades[]);

/**
 * Shows a simple and clean main
 * @return zero if the application executes successfully
 */
int main() {
	// declares the array
	char grades[NUMBER_OF_GRADES];

	cout << "Please type in five grades (A,B,C,D, or F)\n";
	getGrades(grades);
	cout << "You typed\n";
	displayGrades(grades);
	return 0;
}

void getGrades(char grades[]) {
	for(int i = 0; i < NUMBER_OF_GRADES; i++) {
		cout << "Grade " << (i+1) << ": ";
		cin >> grades[i];
	}
}

void displayGrades(const char grades[]) {
	for(int i = 0; i < NUMBER_OF_GRADES; i++) {
		cout << "Grade " << (i+1) << " = " 
		     << static_cast<char>(toupper(grades[i])) << endl;
	}
	// this call to averageGrades below will cause an error
	// can you figure out WHY???
	// HINT:	look at the parameter list in displayGrades
	//		Now look at the parameter list in averageGrades
	//cout << "The average grade is " << averageGrades(grades) << endl;
}

double averageGrades(char grades[]) {
	int sum = 0;
	for(int i = 0; i < NUMBER_OF_GRADES; i++) {
		if(toupper(grades[i]) == 'A') {
			sum += 4;
		} else if(toupper(grades[i]) == 'B') {
			sum += 3;
		} else if(toupper(grades[i]) == 'C') {
			sum += 2;
		} else if(toupper(grades[i]) == 'D') {
			sum += 1;
		}
	}
	// since sum is an int and NUMBER_OF_GRADES is
	// an int, we have to preserve any decimal digits 
	// that are a result of the division by casting one
	// operator to a double
	return static_cast<double>(sum)/NUMBER_OF_GRADES;
}
